<?php
	session_start();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Register Form</title>
    <link href="css/auth.css" rel="stylesheet" type="text/css" />
</head>

<script type="text/javascript">
// <![CDATA[
            
function chkPasswd() {
	
	form = document.register;
    
    var input = document.getElementById("passwdchk");

    if (input) {
        input.className=(form.password.value !== form.cpassword.value)?'notvalidinput':'validinput';
        input.className=(!validateTextfield(form.password,true,false))?'hidden':input.className;
        input.className=(!validateTextfield(form.cpassword,true,false))?'hidden':input.className;
    }
          enableSubmit();  
}

function chkFname() {
    
	form = document.register;
    
    var input = document.getElementById("fnamechk");
    
    if (input)  input.className=(validateTextfield(form.fname,true,true))?'validinput':'notvalidinput';
	
	enableSubmit();
}

function chkLname() {
    
	form = document.register;    
    
    var input = document.getElementById("lnamechk");
    
    if (input) input.className=(validateTextfield(form.lname,true,true))?'validinput':'notvalidinput';
	
	enableSubmit();
}

function chkUsername() {
    
	form = document.register;
    
    var input = document.getElementById("usernamechk");
    
    if (input) input.className=(validateTextfield(form.username,true,false))?'validinput':'notvalidinput';

	enableSubmit();
}

function enableSubmit(){

	form = document.register;
	
	if(	(validateTextfield(form.username, false, false))
		&& (validateTextfield(form.lname, false, true))
		&& (validateTextfield(form.fname, false, true))
		&& ("" != form.cpassword.value) 
		&& (form.password.value == form.cpassword.value)
		) {
        document.getElementById("doRegister").disabled = false;
		document.getElementById("doRegister").style.color = "#030303";
	} else {
		document.getElementById("doRegister").disabled = true;
		document.getElementById("doRegister").style.color = "#7A7A7A";
	}
}
function validateTextfield(fld,warn,spaces) {
    var error = "";
	
	if(spaces) {
		var validChars = /^[a-zA-Z0-9\-\_\s]{2,}$/;
	} else {
		var validChars = /^[a-zA-Z0-9\-\_]{2,}$/;
	}

    if (fld.value == "") {
        if(warn) fld.style.background = '#FFCC66'; 
        error = "Empty field!.\n";
    } else if ((fld.value.length < 4) || (fld.value.length > 20)) {	
		if(warn) fld.style.background = '#FFCC66'; 
        error = "Wrong size!.\n";
	} else if (!validChars.test(fld.value)) {
		if(warn) fld.style.background = '#FFCC66'; 
        error = "mmm.. are you trying something?.\n";
    } else {
		if(warn) fld.style.background = '#bdeaff';
		return true;
    } 
    return false;
}

// ]]>
</script>
<body>

<form id="register" class="register" name="register" method="post" action="register-exec.php">

      <label>First Name </label>
      <input name="fname" type="text" class="textfield" maxlength="20" id="fname" value="<?php echo $_SESSION['REG_FNAME'] ?>" onkeyup="chkFname()"/><div id="fnamechk" class="hidden"></div>
      
      <label>Last Name </label>
      <input name="lname" type="text" class="textfield" id="lname" maxlength="20" value="<?php echo $_SESSION['REG_LNAME'] ?>" onkeyup="chkLname()"/><div id="lnamechk" class="hidden"></div>
      
      <label>Username</label>
      <input name="username" type="text" class="textfield" id="username" maxlength="20" value="<?php echo $_SESSION['REG_UNAME'] ?>" onkeyup="chkUsername()"/><div id="usernamechk" class="hidden"></div>
      
      <label>Password</label>
      <input name="password" type="password" class="textfield" id="password" onkeyup="chkPasswd()" maxlength="32"/>
      
      <label>Confirm Password</label>
      <input name="cpassword" type="password" class="textfield" id="cpassword" onkeyup="chkPasswd()" maxlength="32"/><div id="passwdchk" class="hidden"></div>
        
      <input id="doRegister" type="submit" class="submit" name="Submit" value="Register" disabled="true"/>

</form>

<?php

    if( isset($_SESSION['ERRMSG_ARR']) && is_array($_SESSION['ERRMSG_ARR']) && count($_SESSION['ERRMSG_ARR']) >0 ) {
	
        echo '<div class="error">';

        foreach($_SESSION['ERRMSG_ARR'] as $msg) {
			#echo '<li>',$msg,'</li>'; 
            echo $msg.'<br />';
        }
		
        echo '</div>';
		unset($_SESSION['ERRMSG_ARR']);
        
	} else {
    
        $_SESSION['REG_FNAME']='';
        $_SESSION['REG_LNAME']='';
        $_SESSION['REG_UNAME']='';
    
    }
    
?>

</body>
</html>
